package com.example.pclimb.repository;

import android.app.Application;

import androidx.lifecycle.LiveData;

import com.example.pclimb.dao.UserLikeDao;
import com.example.pclimb.database.PclimbDatabase;
import com.example.pclimb.model.UserLike;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 用户点赞记录数据仓库，处理用户点赞相关的所有数据操作
 */
public class UserLikeRepository {
    private UserLikeDao userLikeDao;
    private ExecutorService executorService;

    public UserLikeRepository(Application application) {
        PclimbDatabase database = PclimbDatabase.getInstance(application);
        userLikeDao = database.userLikeDao();
        executorService = Executors.newSingleThreadExecutor();
    }

    /**
     * 添加用户点赞记录
     * @param userLike 用户点赞记录
     */
    public void insert(UserLike userLike) {
        executorService.execute(() -> userLikeDao.insert(userLike));
    }

    /**
     * 删除用户点赞记录
     * @param userLike 用户点赞记录
     */
    public void delete(UserLike userLike) {
        executorService.execute(() -> userLikeDao.delete(userLike));
    }

    /**
     * 删除用户点赞记录
     * @param userId 用户ID
     * @param guideId 攻略ID
     */
    public void deleteUserLike(int userId, int guideId) {
        executorService.execute(() -> userLikeDao.deleteUserLike(userId, guideId));
    }

    /**
     * 检查用户是否点赞了攻略
     * @param userId 用户ID
     * @param guideId 攻略ID
     * @return 是否点赞的LiveData
     */
    public LiveData<Integer> isLiked(int userId, int guideId) {
        return userLikeDao.isLiked(userId, guideId);
    }

    /**
     * 获取用户点赞记录
     * @param userId 用户ID
     * @param guideId 攻略ID
     * @return 用户点赞记录的LiveData
     */
    public LiveData<UserLike> getUserLike(int userId, int guideId) {
        return userLikeDao.getUserLike(userId, guideId);
    }

    /**
     * 获取攻略的点赞数
     * @param guideId 攻略ID
     * @return 点赞数的LiveData
     */
    public LiveData<Integer> getLikeCount(int guideId) {
        return userLikeDao.getLikeCount(guideId);
    }

    /**
     * 获取用户的点赞数
     * @param userId 用户ID
     * @return 点赞数的LiveData
     */
    public LiveData<Integer> getUserLikeCount(int userId) {
        return userLikeDao.getUserLikeCount(userId);
    }
} 